package bstsetcode.homework;




/**
 * @author noob
 * @version 1.0
 * @date 2021/3/13 21:12
 *  从后向前实现选择排序
 */
public class SelectionSort01 {

    public static <E extends Comparable<E>> void sort(E[] data){

        for (int i = data.length-1; i >= 0 ; i--) {
            int maxIndex=i;
            for (int j = i; j >= 0; j--) {
                if(data[j].compareTo(data[maxIndex]) > 0){
                    maxIndex = j;
                }
            }

            exchange(data,i,maxIndex);
        }
    }

    private static <E> void exchange(E[] data, int i, int j) {
        E temp = data[i];
        data[i] =data[j];
        data[j] = temp;
    }

    public static void main(String[] args) {
        Integer[] data2 = {6,4,2,3,2,1,5,5,};
        SelectionSort01.sort(data2);
        for (int n:data2){
            System.out.print(n +" ");
        }
        System.out.println();

        Student[] students = {
                new Student("faker", 99),
                new Student("bang", 90),
                new Student("bengi", 99),
                new Student("wolf",96)
        };
        SelectionSort01.sort(students);
        for (Student student:students){
            System.out.print(student+" ");
        }
        System.out.println();
    }


}
